package com.baikeyang.modules.util;

import com.baikeyang.common.util.StringUtils;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;

import java.math.BigInteger ;

/**
 * Created by lenovo on 2018/7/30.
 */
public class UtilTests1 {

    @Test
    public void test1() {
        List<Long> longs = new ArrayList<Long>();
        longs.add(9L);
        longs.add(65L);
        System.out.println(longs.toString());
        List<String> cities = Arrays.asList("Milan",
                "London",
                "New York",
                "San Francisco");
        String citiesCommaSeparated = StringUtils.join(longs, ",");
        System.out.println(citiesCommaSeparated);
    }

    @Test
    public void test2() {
        String str = "0102030405060708";
        String result = "";
        char[] chars = str.toCharArray();
        for(int i = 0; i < chars.length; i++){
            result += Integer.toBinaryString(chars[i]);
        }
        System.out.println(result);
    }



    /**
     * 字符串转换成十六进制字符串
     */
    public static String str2HexStr(String str) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString();
    }

    /**
     *
     * 十六进制转换字符串
     */

    public static byte[] hexStr2Bytes(String hexStr) {
        System.out.println("in len :" + hexStr.length());
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        System.out.println("out len :" + bytes.length);
        System.out.println("ddd" + Arrays.toString(bytes));
        return bytes;
    }

    /**
     * bytes转换成十六进制字符串
     */
    public static String byte2HexStr(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
            // if (n<b.length-1) hs=hs+":";
        }
        return hs.toUpperCase();
    }

    public static String encrypt(String key, String initVector, String value) {
        try {
//            System.out.println("key:\t" + Arrays.toString(key.getBytes("UTF-8")));
//            System.out.println("iv:\t" + Arrays.toString(initVector.getBytes("UTF-8")));
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            //Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());
            //System.out.println(Arrays.toString(encrypted));
//            System.out.println("encrypted string: "
//                    + Base64.encodeBase64String(encrypted));

            //return byte2HexStr(encrypted);
            return Base64.encodeBase64String(encrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public static String decrypt(String key, String initVector, String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            //byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
            byte[] original = cipher.doFinal(hexStr2Bytes(encrypted));

            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }


    public static String modulus = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7";
    public static String nonce = "0CoJUm6Qyw8W8jud";
    public static String pubKey = "010001";

    public static void main(String[] args) {
        String key = nonce; // 128 bit key
        String initVector = "0102030405060708"; // 16 bytes IV

        String en = encrypt(key, initVector, "{\"csrf_token\":\"\"}");
        //System.out.println(en);
        String key2 = "qAdKCoCWtwSGVxCf";
        String encText = encrypt(key2, initVector, en);
        System.out.println("encText:"+encText);


        String _text = new StringBuffer(key2).reverse().toString();
        String[] _texts = _text.split("");
        String str = StringUtils.join(_texts, "");
        BigInteger biText = new BigInteger(strTo16(str), 16);
        BigInteger biEx = new BigInteger(pubKey, 16);
        BigInteger biMod = new BigInteger(modulus, 16);

        // create 3 BigInteger objects
        BigInteger biRet = biText.modPow(biEx, biMod);




        String encSecKey = biRet.toString(16);
        System.out.println("encSecKey:"+zfill(encSecKey, 256));


        //System.out.println(decrypt(key, initVector, en));
    }

    public static String zfill(String str, Integer size) {
        while (str.length() < size) str = "0" + str;
        return str;
    }

    /**
     * 字符串转化成为16进制字符串
     * @param s
     * @return
     */
    public static String strTo16(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }



    @Test
    public void rsaEncrypt(String text, String pubKey, String modulus) {
        String _text = "YXtt755px6h5g4nDY5Ngm1/IyPHeUiJeQl/KoDs9KTxAwHx8NdSB7PIXjIiivWws";

        String[] _texts = _text.split("");
        System.out.println(_texts);
        //String _text = text.split("").reverse().join('');
//        var biText = bigInt(new Buffer(_text).toString('hex'), 16),
//                biEx = bigInt(pubKey, 16),
//                biMod = bigInt(modulus, 16),
//                biRet = biText.modPow(biEx, biMod);
//        return zfill(biRet.toString(16), 256);
    }

}
